﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ColdBootNinject
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			var kernel = new Ninject.Core.StandardKernel(new SampleModule());

			Action action = () =>
			{
				var webApp = kernel.Get<IWebApp>();
				webApp.Run();
			};

			action();

			Close();
		}

		class SampleModule : Ninject.Core.StandardModule
		{
			public override void Load()
			{
				Bind<IWebApp>().ToMethod(
					c => new WebApp(
						c.Kernel.Get<IAuthenticator>(),
						c.Kernel.Get<IStockQuote>()))
					.Using<Ninject.Core.Behavior.TransientBehavior>();

				Bind<IAuthenticator>().ToMethod(
					c => new Authenticator(
						c.Kernel.Get<ILogger>(),
						c.Kernel.Get<IErrorHandler>(),
						c.Kernel.Get<IDatabase>()))
					.Using<Ninject.Core.Behavior.TransientBehavior>();

				Bind<IStockQuote>().ToMethod(
					c => new StockQuote(
						c.Kernel.Get<ILogger>(),
						c.Kernel.Get<IErrorHandler>(),
						c.Kernel.Get<IDatabase>()))
					.Using<Ninject.Core.Behavior.TransientBehavior>();

				Bind<IDatabase>().ToMethod(
					c => new Database(
						c.Kernel.Get<ILogger>(),
						c.Kernel.Get<IErrorHandler>()))
					.Using<Ninject.Core.Behavior.TransientBehavior>();

				Bind<IErrorHandler>().ToMethod(
					c => new ErrorHandler(c.Kernel.Get<ILogger>()))
					.Using<Ninject.Core.Behavior.TransientBehavior>();

				Bind<ILogger>().ToMethod(c => new Logger())
					.Using<Ninject.Core.Behavior.TransientBehavior>();
			}
		}
	}
}